home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15737 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.4 KB

  1. Path: lrz-muenchen.de!news
  2. From: watzka@stat.uni-muenchen.de (Kurt Watzka)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Probs with prototype mixes with Functions
  5. Date: 21 Apr 1996 14:51:59 GMT
  6. Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
  7. Distribution: world
  8. Message-ID: <4ldi2f$6ed@sparcserver.lrz-muenchen.de>
  9. References: <dward.1.000DC9CA@melbpc.org.au> <31790B9E.3EE@ix.netcom.com>
  10. NNTP-Posting-Host: sun2.lrz-muenchen.de
  11.  
  12. Jane Harper <jharper@ix.netcom.com> writes:
  13.  
  14. >Darren Ward wrote:
  15. >> 
  16. >> Could anyone help me with the following?
  17. >> 
  18. >> when making a function such as:
  19. >> 
  20. >> float convert(int celcius)
  21. >> {
  22. >>     float faranheit;
  23. >>     faranheit = ((float)celcius*9/5)+32;
  24. >>     return faranheit;
  25. >> }
  26.  
  27. >When you multiply celcius (cast to float) by 9/5 (an integer division), 
  28. >celcius is cast back to an int.  Therefore, your return is an int, not a 
  29. >float.  If you want a float return, change 9/5 to 9.0/5.0.  :D
  30.  
  31. While there indeed _is_ a potential problem using 9/5 to represent 1.8,
  32. a float will _never_ be cast back to an int during the evaluation of
  33. an expression if the expression is anything but an assignment expression.
  34.  
  35. So, if you write 
  36.  
  37.   float fahrenheit;
  38.   int celsius;
  39.  
  40.   fahrenheit = (float) celsius * 9;
  41.  
  42. you can be sure that the multiplication is done with either float or
  43. double prcision, but _not_ using integer arithmetic.
  44.  
  45. Kurt
  46. --
  47. | Kurt Watzka                             Phone : +49-89-2180-6254
  48. | watzka@stat.uni-muenchen.de
  49.